package com.project.util;

import java.io.*;

/**
 * 完成文件拷贝
 */
public class CopyFile {

    /**
     * 拷贝文件
     * @param sourcePath 源文件路径
     * @param targetDirPath 目标文件路径
     * @return 重命名的文件名
     */
    public static String copy(String sourcePath,String targetDirPath){
        String targetFileName = System.currentTimeMillis()+sourcePath.substring(sourcePath.lastIndexOf("."));
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(sourcePath);
            out = new FileOutputStream(targetDirPath+"/"+targetFileName);

            byte[] by = new byte[1024];
            int len = 0;
            while ((len=in.read(by))!=-1){
                out.write(by,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return targetFileName;
    }
}
